<template>

  <el-dialog
    :title="title"
    :visible.sync="show"
    width="30%"
    @close="close"
  >
    <el-form ref="form" :model="param" :rules="rules" class="form" label-position="right" label-width="80px">
      <el-form-item label="参数名称" prop="paramName">
        <el-input v-model="param.paramName" />
      </el-form-item>
      <el-form-item label="参数Key" prop="paramKey">
        <el-input v-model="param.paramKey" />
      </el-form-item>
      <el-form-item label="参数值" prop="paramValue">
        <el-input v-model="param.paramValue" />
      </el-form-item>
      <el-form-item label="状态" prop="state">
        <el-radio v-model="param.state" :label="false">停用</el-radio>
        <el-radio v-model="param.state" :label="true">正常</el-radio>
      </el-form-item>
      <el-form-item label="备注" prop="note">
        <el-input v-model="param.note" type="textarea" placeholder="请选择" :rows="2" />
      </el-form-item>
      <el-row class="form-button">
        <el-button type="primary" @click="save">确定</el-button>
        <el-button @click="close">取消</el-button>
      </el-row>
    </el-form>
  </el-dialog>
</template>

<script>
import { find, save, update } from '@/api/sys/param'

export default {
  name: 'ParamForm',
  props: {
    id: {
      type: Number,
      default: null
    },
    title: {
      type: String,
      default: '添加系统参数'
    },
    show: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      param: { paramName: '', paramKey: '', paramValue: '', state: true, note: '' },
      options: [{ label: '停用', value: 0 }, { label: '正常', value: 1 }],
      rules: {
        paramName: [
          { required: true, message: '请输入参数名称', trigger: 'blur' }
        ],
        paramKey: [
          { required: true, message: '请输入参数Key', trigger: 'blur' }
        ],
        paramValue: [
          { required: true, message: '请输入参数值', trigger: 'blur' }
        ]
      }
    }
  },
  watch: {
    id(val) {
      if (val) { this.find() }
    }
  },
  methods: {
    close() {
      this.$refs['form'].resetFields()
      this.param = { paramName: '', paramKey: '', paramValue: '', state: true, note: '' }
      this.$emit('update:show', false)
      this.$emit('update:id', null)
    },
    save() {
      this.$refs['form'].validate(async(valid) => {
        if (valid) {
          if (this.id) {
            await update(this.param)
            this.responseRes()
          } else {
            await save(this.param)
            this.responseRes()
          }
        }
      })
    },
    async find() {
      const { data } = await find(this.id)
      this.param = data
    },
    responseRes() {
      this.close()
      this.$parent.findPage()
    }
  }
}
</script>

<style scoped>

</style>
